package logic

import (
	"context"
	"person/models/mysql"

	"person/perrpc/internal/svc"
	"person/perrpc/perrpc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *perrpc.LoginRequest) (*perrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	user := mysql.User{
		Model:    gorm.Model{},
		Account:  in.Account,
		Password: in.Password,
	}

	if err := user.FIndUser(mysql.DB, in.Account); err != nil {
		return nil, err
	}
	return &perrpc.LoginResponse{
		UserId: int64(user.ID),
	}, nil
}
